<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2018/6/21
 * Time: 17:53
 */

namespace app\api\controller;


class Note extends Api
{
    //我的笔记
    public function lists(){
        $id =$this->cache['id'];

        $page = input('post.pageindex');
        $pageSize = input('post.pagesize');

        //判定ID不否存在
        if(!$id && !is_numeric($id)){
           return show(false,0,'ID不能为空');
        }

        //根据ID入库查询
        try{
            $notes = model("Notes")->getNotesById($id,$page,$pageSize);
            $count = model("Notes")->getNotesCount($id);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }
            $PageCom = count($notes);
        //拼接数据
        $result = array(
            'CurrentCount' => $PageCom,
            'IsNextPage' => '',
            'IsPreviousPage' => '',
            'Items' => self::GetNotes($notes),
            'PageIndex' => $page,
            'PageSize' => $pageSize,
            'TotalCount' => $count,
            'TotalPages' => '',
        );

        return show(true,0,'',$result);
    }

    public function GetNotes($data){
        if(!$data && !is_array($data)){
            return [];
        }
        $res = array();
        foreach($data as $k => $v){
           $res[$k]['Content'] = $v['content'];
           $res[$k]['CreateTimeDesc'] = $v['create_time'];
           $res[$k]['ContentDesc'] = $v['content'];
           $res[$k]['CourseId'] = $v['CourseId'];
           $res[$k]['Id'] = $v['id'];
           $res[$k]['Index'] = 1;
           $res[$k]['Long'] = '';
        }
        return $res;
    }

    //添加笔记
    public function add(){
        //获取当前用户
        $res['user_id'] = $this->cache['id'];
        if(!$res['user_id'] && !is_numeric($res['user_id'])){
            return show(false,0,'请先登陆');
        }
        //获取课程ID
        $res['course_id'] = input('post.courseId');
        //获取音频ID
        $res['audio_id'] = input('post.audioId');
        //获取笔记内容
        $res['content'] = input('post.content');
        $res['create_time'] = time();

        //入库操作
        try{
            $id = model("Notes")->getNoteAdd($res);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        if(!$id && !is_numeric($id)){
            return show(false,0,'添加失败');
        }

        return show(true,0,'OK','id='.$id.'的数据添加成功');
    }

    //获取笔记信息
    public function get(){
        //获取笔记ID
        $id = input('post.id');

        //入库操作
        try{
            $note = model("Notes")->getNoteByNoteId($id);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        //拼接数据
        $res = array(
            'Content' => $note[0]['content'],
            'CreateTimeDesc' => $note[0]['create_time'],
            'ContentDesc' => $note[0]['content'],
            'CourseId' => $note[0]['CourseId'],
            'Id' => $note[0]['id'],
            'Index' => 1,
            'Long' => '',
        );

        return show(true,0,'OK',$res);

    }

    //修改笔记
    public function edit(){
        //获取笔记ID
        $nid = input('post.id');
        //获取修改的内容
        $data['content'] = input('post.content');

        //入库操作
        try{
            $res = model('Notes')->EditNoteById($nid,$data);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        return show(true,0,'');
    }

    //删除笔记
    public function del(){
        //获取笔记ID//获取笔记ID
        $nid = input('post.id');
        //获取修改的内容
        $data['status'] = -1;

        //入库操作
        try{
            $res = model('Notes')->EditNoteById($nid,$data);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }
        return show(true,0,'');

    }

}